package com.zuxia.impDao;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.map.HashedMap;
import org.apache.struts2.ServletActionContext;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;

import com.zuxia.iDao.AdmininfoDAOI;
import com.zuxia.po.Admininfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Admininfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.zuxia.impDao.Admininfo
 * @author MyEclipse Persistence Tools
 */

public class AdmininfoDAO extends HibernateDaoSupport implements AdmininfoDAOI {
	private static final Logger log = LoggerFactory
			.getLogger(AdmininfoDAO.class);
	// property constants
	public static final String AINAME = "ainame";
	public static final String AISEX = "aisex";
	public static final String AIPWD = "aipwd";
	public static final String AIPHONE = "aiphone";
	public static final String AIID_CARD = "aiidCard";
	public static final String AIADDRESS = "aiaddress";

	protected void initDao() {
		// do nothing
	}

	/**
	 * 添加数据
	 */
	public void save(Admininfo transientInstance) {
		log.debug("saving Admininfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Admininfo persistentInstance) {
		log.debug("deleting Admininfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * 查询管理员，返回map用于存为session
	 * @param phone
	 * @param pwd
	 * @return
	 */
	public Map<String, Object> selUserByPhoneAndPwd(String phone, String pwd) {
		Map<String, Object> mapForSession = new HashedMap();
		String queryString = "select a.aiid,a.ainame,a.powerinfo.piname from Admininfo as a  where a.aiphone=? and a.aipwd=?";
		List listAdmin = getHibernateTemplate().find(queryString, phone,pwd);
		Iterator iter = listAdmin.iterator();
		while (iter.hasNext()) {
			//由于上面是只查询管理员编号和姓名，所以数据被hib封装在了一个object数组中了
			Object[] obj = (Object[])iter.next();
			System.out.println("登录成功：编号："+obj[0]+"姓名："+obj[1]+"权限："+obj[2]);
			//封装进Map
			mapForSession.put("id", obj[0]);
			mapForSession.put("name", obj[1]);
			mapForSession.put("piname", obj[2]);
		}
		return mapForSession;
	}
//根据传入的账号密码查询是否存在
	public Boolean userShowPwd(String name, String pwd) {
		String queryString =" from Admininfo as a  where a.ainame=? and a.aipwd=?  ";
		List listAdmin = getHibernateTemplate().find(queryString, name,pwd);
		if(listAdmin.size()>0){
			return true;
		}
		return false;
	}
	
	
	public Admininfo findById(java.lang.Integer id) {
		log.debug("getting Admininfo instance with id: " + id);
		try {
			Admininfo instance = (Admininfo) getHibernateTemplate().get(
					"com.zuxia.impDao.Admininfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	

	/**
	 * 根据实体返回数据
	 */
	public List findByExample(Admininfo instance) {
		log.debug("finding Admininfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* 根据对应属性的对应值查找数据
	 * @see com.zuxia.impDao.AdmininfoDAOI#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Admininfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Admininfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* 根据指定的管理员属性对应的值查找数据
	 * @see com.zuxia.impDao.AdmininfoDAOI#findByAiname(java.lang.Object)
	 */
	public List findByAiname(Object ainame) {
		return findByProperty(AINAME, ainame);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.AdmininfoDAOI#findByAisex(java.lang.Object)
	 */
	public List findByAisex(Object aisex) {
		return findByProperty(AISEX, aisex);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.AdmininfoDAOI#findByAipwd(java.lang.Object)
	 */
	public List findByAipwd(Object aipwd) {
		return findByProperty(AIPWD, aipwd);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.AdmininfoDAOI#findByAiphone(java.lang.Object)
	 */
	public List findByAiphone(Object aiphone) {
		return findByProperty(AIPHONE, aiphone);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.AdmininfoDAOI#findByAiidCard(java.lang.Object)
	 */
	public List findByAiidCard(Object aiidCard) {
		return findByProperty(AIID_CARD, aiidCard);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.AdmininfoDAOI#findByAiaddress(java.lang.Object)
	 */
	public List findByAiaddress(Object aiaddress) {
		return findByProperty(AIADDRESS, aiaddress);
	}

	/* 查找管理员实体的数据管理员表
	 * @see com.zuxia.impDao.AdmininfoDAOI#findAll()
	 */
	public List findAll() {
		log.debug("finding all Admininfo instances");
		try {
			String queryString = "from Admininfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Admininfo merge(Admininfo detachedInstance) {
		log.debug("merging Admininfo instance");
		try {
			Admininfo result = (Admininfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Admininfo instance) {
		log.debug("attaching dirty Admininfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Admininfo instance) {
		log.debug("attaching clean Admininfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
//修改密码的方法

	public Boolean userUpdatePwd(String name, String pwd, String xpwd) {
		Boolean b=false;
		Transaction trans=getSession().beginTransaction();
		String HQL="update Admininfo set aipwd=? where aipwd=? and ainame=?";
		Query queryupdate=getSession().createQuery(HQL);
		queryupdate.setString(0, xpwd);
		queryupdate.setString(1, pwd);
		queryupdate.setString(2, name);
		int ret=queryupdate.executeUpdate();
		if(ret>0){
			b=true;
		}
		trans.commit();
		return b;
	}
	//查询个人的全部信息的方法
	public List userSelect(int id) {
		Map<String, Object> mapForSession = new HashedMap();
		String queryString = "from Admininfo as a  where a.aiid=?";
		return getHibernateTemplate().find(queryString, id);
		
		/*Iterator iter = listAdmin.iterator();
		while (iter.hasNext()) {
		}*/
	}

}